package com.feijiang.controller;


import com.feijiang.entity.Experiment;
import com.feijiang.service.IExperimentService;
import com.feijiang.util.ResponseData;
import com.feijiang.vo.QueryVo;
import javax.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/experiment")
public class ExperimentController {
  @Resource
  IExperimentService iExperimentService;

  /**
   * 添加实验接口
   * @param experiment experiment
   * @return ResponseData
   */
  @RequestMapping("/add")
  public ResponseData addExpr(@RequestBody Experiment experiment) {
    boolean add = iExperimentService.add(experiment);
    if(!add) {
      return ResponseData.error("实验添加失败");
    }
    return ResponseData.success(add);
  }

  /**
   * 修改实验接口
   * @param experiment experiment
   * @return ResponseData
   */
  @PostMapping("/edit")
  public ResponseData editExpr(@RequestBody Experiment experiment) {
    if (experiment.getId() == null) {
      return ResponseData.error("缺少ID");
    }
    boolean edit = iExperimentService.edit(experiment);
    if(!edit) {
      return ResponseData.error("实验修改失败");
    }
    return ResponseData.success("实验修改成功");
  }

  /**
   * 删除实验信息接口
   * @param exprId
   * @return
   */
  @PostMapping("/delete/{id}")
  public ResponseData deleteExpr(@PathVariable(value = "id") int exprId) {
    boolean delete = iExperimentService.delete(exprId);
    if(!delete) {
      return ResponseData.error("实验信息删除失败");
    }
    return ResponseData.success("实验信息删除成功");
  }

  /**
   * 分页查询
   * @param pageSize
   * @param pageNum
   * @return
   */
  @GetMapping("/list")
  public ResponseData listExpr(@RequestParam(defaultValue = "10") int pageSize, @RequestParam(defaultValue = "1") int pageNum) {
    Assert.isTrue(pageNum > 0 && pageSize > 0, "请输入正确的参数");
    return ResponseData.success(iExperimentService.list(pageSize, pageNum));
  }

  @GetMapping("/{id}")
  public ResponseData getById(@PathVariable("id") int id) {
    return ResponseData.success(iExperimentService.getById(id));
  }

  @GetMapping("/listAll")
  public ResponseData listAll() {
    return ResponseData.success(iExperimentService.list());
  }

  @PostMapping("/search")
  public ResponseData search(@RequestParam String keyword) {
    return ResponseData.success(iExperimentService.search(keyword));
  }
}
